# -*- coding: utf-8 -*-
# @Time    : 2024/8/29 0:26
# @Author  : Alvin
# @File    : resources.py


from sqlalchemy import Column, Integer, String, Text, ForeignKey, SmallInteger
from sqlalchemy.orm import Relationship

from app.models.base import Base, db
from app.libs.error_code import DataBaseError
class Resource(Base):
    __tablename__ = 'resources'
    users = Relationship('User')
    resource_id = Column(Integer, primary_key=True)
    resource_name = Column(String(100), nullable=False)
    # file_type = Column(SmallInteger)
    description = Column(Text)
    file_path = Column(String(255), nullable=False)
    upload_user_id = Column(Integer, ForeignKey('users.id'))
    # upload_time = Column(DateTime, default=datetime.utcnow)
    download_count = Column(Integer, default=0)
    required_points = Column(Integer)
    category = Column(String(50))


    @staticmethod
    def insert_upload_record(uid, file_name, file_path, description, required_points):
        with db.auto_commit():
            resource = Resource()
            resource.resource_name = file_name
            resource.upload_user_id = uid
            resource.file_path = file_path
            resource.description = description
            resource.required_points = required_points
            db.session.add(resource)


